<?php
include_once( "common.php" );
checkLogin( STATUS_ADMIN );

# Verify the project
if ( ! isValidEntry( "Project", $_POST["id"] ) )
    outputAlert( "Invalid input", "You entered invalid input." );

# Get the project's current information
$result = db_query( "SELECT * From Project WHERE Project.id = {$_POST["id"]}" );
$row = db_fetch_object( $result );

# Change the user's name
if ( trim( $_POST["project"] ) != "" and $row->name != $_POST["project"] )
{
    db_query( "UPDATE Project SET Project.name = '{$_POST["project"]}' WHERE Project.id = {$_POST["id"]}" ) or outputAlert( "Database error", "There was a database error and the project was not updated." );
}

# Delete the project
if ( $_POST["delete"][0] == "delete" and $_POST["delete"][1] == "confirm" )
{
    # Remove comments
    db_query( "DELETE FROM Log WHERE Log.bug IN ( SELECT Bug.id FROM Bug WHERE Bug.project = {$_POST["id"]} )" ) or outputAlert( "Database error", "There was a database error and the project's logs were not deleted." );

    # Remove all references in logs
    db_query( "UPDATE Log SET Log.project = -1 WHERE Log.project = {$_POST["id"]}" ) or outputAlert( "Database error", "There was a database error and the project's related logs were not deleted." );

    # Remove bugs
    db_query( "DELETE FROM Bug WHERE Bug.project = {$_POST["id"]}" ) or outputAlert( "Database error", "There was a database error and the project's bugs were not deleted." );

    # Remove visibility
    db_query( "DELETE FROM UserProject WHERE UserProject.project = {$_POST["id"]}" ) or outputAlert( "Database error", "There was a database error and the project's visibility relationships were not deleted." );

    # Delete the project
    db_query( "DELETE FROM Project WHERE Project.id = {$_POST["id"]}" ) or outputAlert( "Database error", "There was a database error and the project was not deleted." );

    header( "Location: admin.php" );
    exit();
}

# Go back to editing this project
header( "Location: viewProject.php?id={$_POST["id"]}" );
?>
